package com.qf.openapi.web.master.controller;

import com.qf.openapi.web.master.entity.Rout;
import com.qf.openapi.web.master.entity.User;
import com.qf.openapi.web.master.service.RoutService;
import com.qf.openapi.web.master.vo.ResultVo;
import com.qf.openapi.web.master.vo.RoutRegistVo;
import com.qf.openapi.web.master.vo.TableVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.PublicKey;
import java.util.List;

@RestController
@RequestMapping("/rout")
@Slf4j
public class RoutController {
    @Autowired
    RoutService routService;

    @RequestMapping("/selectAll")
    public TableVo<Rout> selectAll(Integer page, Integer limit, String gatewayApiName, String state) {
        TableVo<Rout> selectAll = routService.selectAll(page, limit, gatewayApiName, state);
        return selectAll;

    }

    @RequestMapping("/add")
    public ResultVo addRout(RoutRegistVo routRegistVo) {
        log.debug("接口入参为: rout={}", routRegistVo);
        boolean flag = routService.addRout(routRegistVo);
        if (flag) {
            return ResultVo.ok();
        } else {
            return ResultVo.error("添加失败");
        }
    }

    @RequestMapping("/update")
    public ResultVo updateRout(Rout rout) {
        boolean flag = routService.updateRout(rout);
        if (flag) {
            return ResultVo.ok();
        } else {
            return ResultVo.error("修改失败");
        }
    }

    @RequestMapping("/delete")
    public ResultVo deleteRout(Integer[] ids) {
        boolean flag = routService.deleteRout(ids);
        if (flag) {
            return ResultVo.ok();
        } else {
            return ResultVo.error("删除失败");
        }
    }

    @RequestMapping("/export")
    public ResultVo export(String gatewayApiName, String state) {
        String filename = routService.export(gatewayApiName, state);
        ResultVo ok = ResultVo.ok();
        ok.put("data", filename);
        return ok;
    }

    @RequestMapping("/upload")
    public ResultVo uploadRout(MultipartFile file) throws IOException {
        routService.getListUse(file);
        return ResultVo.ok();
    }
}
